/*********************************************************
* version 1.0 - By Kant@SeekRoad
* 修改日期：2017-02-13
**********************************************************/

var root_url='',app_url='',mod_url='';	  //root_url='localhost';app_url='localhost/app';mod_url='localhost/app/module';
var _menus='',__menus='';
var isShowAdvance = true;           //是否在载入初始页面前显示
var isOpenDefPage = true;           //是否打开初始页面
var defPageType = 'tabs';           //默认页面模式：tabs | single
var defPageUrl = '/index/content';  //初始页面链接
var defPageTitle = '欢迎使用';      //初始页面标题
var defPageIcon = '';               //初始页面图标
var onlyOpenTitle = defPageTitle;   //不允许关闭的标签的标题

window.onload=function(){
	$('#loading-mask').fadeOut();
}
$(function(){
    if($("#navtree").length==0){
	    InitLeftMenu();
    }
    if(defPageType=='tabs'){
        if(isOpenDefPage){addTab(defPageTitle,defPageUrl,defPageIcon,false);}
	    tabCloseEven(); //初始化tabs右键面板
        /* 选择TAB时刷新内容
        $('#tabs').tabs({
            onSelect: function (title) {
                var currTab = $('#tabs').tabs('getTab', title);
                var iframe = $(currTab.panel('options').content);
                var src = iframe.attr('src');
                if(src)
                    $('#tabs').tabs('update', { tab: currTab, options: { content: createTabFrame(src)} });
            }
        }); */
    }else{
        if(isOpenDefPage){refreshPage(defPageUrl,defPageTitle,defPageIcon);}
    }
    if(isShowAdvance){$('#loading-mask').fadeOut();}
})

//初始化左侧
function InitLeftMenu() {
	$("#nav").accordion({animate:false,fit:true,border:false});
	var selectedPanelname = '';
    $.each(_menus.menus, function(i, n){
		var menulist='',fgT='',fgB='',fgTChild='',fgBChild='';
		menulist +='<ul class="navlist">';
        $.each(n.menus, function(j, o){
			if(o.fgset){switch(o.fgset){
			case "11":
				fgT='<li><hr></li>';fgB='<li><hr></li>'; break;
			case "10":
				fgT='<li><hr></li>';fgB=''; break;
			case "01":
				fgT='';fgB='<li><hr></li>'; break;
			default:
				fgT='';fgB='';
			}}
			menulist += fgT+'<li><div ><a ref="'+o.menuid+'" href="javascript:;" rel="' + o.url + '" ><span class="icon '+o.icon+'" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div> ';
			if(o.child && o.child.length>0){
				menulist += '<ul class="third_ul">';
				$.each(o.child,function(k,p){
					if(p.fgset){switch(p.fgset){
					case "11":
						fgTChild='<li><hr></li>';fgBChild='<li><hr></li>'; break;
					case "10":
						fgTChild='<li><hr></li>';fgBChild=''; break;
					case "01":
						fgTChild='';fgBChild='<li><hr></li>'; break;
					default:
						fgTChild='';fgBChild='';
					}}
					menulist += fgTChild+'<li><div><a ref="'+p.menuid+'" href="javascript:;" rel="' + p.url + '" title="' + p.menuname + '" ><span class="icon '+p.icon+'" >&nbsp;</span><span class="nav">' + p.menuname + '</span></a></div> </li>'+fgBChild;
				});
				menulist += '</ul>';
			}
			menulist+='</li>'+fgB;
        })
		menulist += '</ul>';

		$('#nav').accordion('add', {
            title: n.menuname,
            content: menulist,
			border: false,
            iconCls: 'icon '+n.icon
        });

		if(i==0){
			selectedPanelname =n.menuname; //默认选中第一个菜单
        }
    });
	$('#nav').accordion('select',selectedPanelname);
	//初始页面为空时默认使用第一个
	if(defPageUrl==''){
		var panels = $('#nav').accordion('panels');
		var cont = panels[0].panel('options').content;
		defPageUrl = $(cont).find('li a').attr('rel');
	}

	//绑定菜单事件
	$('.navlist li a').click(function(){
		var tabTitle = $(this).children('.nav').text();
		var url = $(this).attr("rel");
		var menuid = $(this).attr("ref");
		var icon = $(this).find('.icon').attr('class');

		var third = find(menuid);
		if(third && third.child && third.child.length>0){
			//$('.third_ul').slideUp();		//点击一个菜单时，前一个菜单分类收起来
			var ul =$(this).parent().next();
			if(ul.is(":hidden"))
				ul.slideDown();
			else
				ul.slideUp();
		}else{
            if(defPageType=='tabs'){
			    addTab(tabTitle,url,icon);
            }else{
			    refreshPage(url,tabTitle,icon);
            }
			$('.navlist li div').removeClass("selected");
			$(this).parent().addClass("selected");
		}
	}).hover(function(){
		$(this).parent().addClass("hover");
	},function(){
		$(this).parent().removeClass("hover");
	});

	$('.third_ul').slideDown();		//二级菜单分类全部展开
}
//获取左侧导航的对象
function find(menuid){
	var obj=null;
	$.each(_menus.menus, function(i, n) {
		 $.each(n.menus, function(j, o) {
		 	if(o.menuid==menuid){
				obj = o;
                return false;
			}
		 });
	});
	return obj;
}
//获取左侧导航的图标
function getIcon(menuid){
	var icon = 'icon ';
	$.each(_menus.menus, function(i, n) {
		 $.each(n.menus, function(j, o) {
		 	if(o.menuid==menuid){
				icon += o.icon;
                return false;
			}
		 })
	})
	return icon;
}

//for single 单页面使用的函数
function refreshPage(url,subtitle,icon){
	//$('#mainContent').panel('refresh', url);
	$('#center_loading').fadeIn(0);
	$('#mainContent').attr('src', url);
	$("#mainContent").load(function(){
		refreshPageHeight();
		//$(window).resize(refreshPageHeight);
		$('#center_loading').fadeOut(1);
	});
}
//for single 获取并重置框架高度
function refreshPageHeight() {
	var ifm = document.getElementById("mainContent");
	var subWeb = document.frames ? document.frames["mainContent"].document : ifm.contentDocument;
	if (ifm != null && subWeb != null) {
		//ifm.height = subWeb.body.scrollHeight;
		var mainheight = $(window).height()-91;
		var frameheight = $(subWeb).find('div').first().height()+35;
		ifm.height = mainheight>frameheight ? mainheight : frameheight;  // 兼容frame内容变少时高度可以自适应减少
	}
}

//for tabs 标签页使用的函数
function addTab(subtitle,url,icon){
    var defClosable=true;
	if(typeof(arguments[3])!="undefined"){defClosable=arguments[3];};
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content:createTabFrame(url),
			closable:defClosable,
			icon:icon
		});
	}else{
		$('#tabs').tabs('select',subtitle);
		$('#tabs_mm-tabupdate').click();
	}
	tabClose();
}
function createTabFrame(url){
	var s = '<iframe scrolling="auto" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>';
	return s;
}
//for tabs 绑定事件
function tabClose(){
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close',subtitle);
	})
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu',function(e){
		$('#tabs_mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs_mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}
//for tabs 绑定右键菜单事件
function tabCloseEven() {
    $('#tabs_mm').menu({
        onClick: function (item) {
            closeTab(item.id);
        }
    });
    return false;
}
function closeTab(action){
    var alltabs = $('#tabs').tabs('tabs');
    var currentTab =$('#tabs').tabs('getSelected');
	var allTabtitle = [];
	$.each(alltabs,function(i,n){
		allTabtitle.push($(n).panel('options').title);
	})

    switch (action) {
        case "refresh":
            var iframe = $(currentTab.panel('options').content);
            var src = iframe.attr('src');
            $('#tabs').tabs('update', {
                tab: currentTab,
                options: {
                    content: createTabFrame(src)
                }
            })
            break;
        case "close":
            var currtab_title = currentTab.panel('options').title;
            $('#tabs').tabs('close', currtab_title);
            break;
        case "closeall":
            $.each(allTabtitle, function (i, n) {
                if (n != onlyOpenTitle){
                    $('#tabs').tabs('close', n);
				}
            });
            break;
        case "closeother":
            var currtab_title = currentTab.panel('options').title;
            $.each(allTabtitle, function (i, n) {
                if (n != currtab_title && n != onlyOpenTitle)
				{
                    $('#tabs').tabs('close', n);
				}
            });
            break;
        case "closeright":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);

            if (tabIndex == alltabs.length - 1){
                alert('亲，后边没有啦 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i > tabIndex) {
                    if (n != onlyOpenTitle){
                        $('#tabs').tabs('close', n);
					}
                }
            });
            break;
        case "closeleft":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);
            if (tabIndex == 1) {
                alert('亲，前边那个上头有人，咱惹不起哦。 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i < tabIndex) {
                    if (n != onlyOpenTitle){
                        $('#tabs').tabs('close', n);
					}
                }
            });
            break;
        case "exit":
            $('#closeMenu').menu('hide');
            break;
    }
}


//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
	$.messager.alert(title, msgString, msgType);
}
